var app = getApp()
var gb = app.globalData
var Bmob = gb.Bmob
var SessionMgr = gb.SessionMgr
var Define = require("../../lib/Define.js")
var _wareId=0
var _sessionUser = null
var _sharedTitle = null

Page({
  data:{
    s_width:app.globalData.screen.width,
    s_half_width:0,
    item_img_width:0,
    ware:{},
    images:[],
    properties:[]
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    this.setData({
      s_half_width:app.globalData.screen.width/2,
      item_img_width:(app.globalData.screen.width-20)
    })

    _wareId = options.wareId
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
    this.requestGetWare()
  },
  onShow:function(){
    // 生命周期函数--监听页面显示
    _sessionUser = Define.getStoredUserInfo()
    console.log(_sessionUser)
  },
  requestGetWare:function(){
    
    var self = this
    wx.request({
      url: Define.value.baseUrl+'/app/ware/detail',
      data: {wareId:_wareId},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        if(res.data.code == 200){
          var data = res.data.data
          data.uDateStr = Define.dateFrom(data.uDate,"yyyy-MM-dd")
          data.mainImage = data.images[0].url
          var images = []
          for(var index = 1 ; index < data.images.length ; index++){
              var item = data.images[index]
              images.push(item.url);
          }

          var results = []
          if(data.properties.length){
            var as = data.properties.split("[||]")
            for(var i = 0 ; i < as.length ; i++){
              var item = as[i]
              var bs = item.split("[:]")
              var key = bs[0]
              var value = bs[1]
              results.push({
                key:key,
                value:value
              })
            }
          }

          self.setData({
            ware:data,
            images:images,
            properties:results
          })

          _sharedTitle = data.title

        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  callPhone:function(event){
    //拨打电话
      console.log(event)
  },
  onMakeOrder:function(event){
    //立即下单

    var self = this

    if (!_sessionUser) {
      wx.navigateTo({
        url: '../setphone/setphone'
      })
      return
    }
    
    wx.navigateTo({
      url: "../inputPhone/inputPhone?wareId=" + _wareId + "&phone=" + _sessionUser.phone
    })

  },
  sendmsg:function(){
    wx.navigateTo({
      url: '../sendmsg/sendmsg',
      success: function(res){
        // success
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: _sharedTitle,
      path: "/pages/wareDetail/wareDetail?wareId="+_wareId
    }
  }
})